package com.gugee.tiktok.common.model.es;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.gugee.tiktok.common.model.mysql.Aweme;
import com.gugee.tiktok.common.model.mysql.Hashtag;
import com.gugee.tiktok.common.model.spider.aweme.Text_extra;
import com.xiguaji.boot.toolkit.JsonUtil;
import com.xiguaji.boot.toolkit.MapperUtils;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class ESAwemeUpsert {
    private long id;
    private long awemeId;
    private String title;
    private long createTime;
    private long uid;
    private long musicId;
    private int commentCount;
    private long diggCount;
    private int downloadCount;
    private long playCount;
    private int shareCount;
    private int rate;
    private String shareUrl;
    private int duration;
    private String region;
    private boolean isAds;
    private String cover;
    private String dynamicCover;
    private Double score;
    private String awemeType;
    private String bloggerType;
    private long updateTime;
    private String uniqueId;
    private List<Hashtag> challenges;

    @Mapper(componentModel = "spring")
    public interface ESAwemeMapper {
        @Mapping(source = "id", target = "id")
        @Mapping(source = "OAwemeId", target = "awemeId")
        @Mapping(source = "ODesc", target = "title")
        @Mapping(source = "OCreateTime", target = "createTime")
        @Mapping(source = "OUid", target = "uid")
        @Mapping(source = "OMusicId", target = "musicId")
        @Mapping(source = "OCommentCount", target = "commentCount")
        @Mapping(source = "ODiggCount", target = "diggCount")
        @Mapping(source = "ODownloadCount", target = "downloadCount")
        @Mapping(source = "OPlayCount", target = "playCount")
        @Mapping(source = "OShareCount", target = "shareCount")
        @Mapping(source = "ORate", target = "rate")
        @Mapping(source = "OShareUrl", target = "shareUrl")
        @Mapping(source = "ODuration", target = "duration")
        @Mapping(source = "ORegion", target = "region")
        @Mapping(source = "OIsAds", target = "isAds")
        @Mapping(source = "OCover", target = "cover")
        @Mapping(source = "ODynamicCover", target = "dynamicCover")
        @Mapping(source = "score", target = "score")
        @Mapping(target = "awemeType", expression = "java(switchIfNull(dto))")
        @Mapping(source = "bloggerType", target = "bloggerType")
        @Mapping(target = "challenges" ,expression = "java(textExtra2List(dto.getOTextExtra()))")
        ESAwemeUpsert mapper(Aweme dto);

        /**
         * 如果视频未分类，则暂时用播主分类代替
         * @param dto
         * @return
         */
        default String switchIfNull(Aweme dto){
            if(StringUtils.isEmpty(dto.getAwemeType())){
                return dto.getBloggerType();
            }
            return dto.getAwemeType();
        }
        default List<Hashtag> textExtra2List(String textExtras) {
            try {
                if (StringUtils.isEmpty(textExtras)) {
                    return new ArrayList<>();
                }
                final ObjectMapper mapper = new ObjectMapper();
                List<Hashtag> hashtagList = mapper.readValue(textExtras, new TypeReference<List<Hashtag>>(){});

                return hashtagList;
            } catch (Exception ex) {
//                System.out.println(ex.getMessage());
                return new ArrayList<>();
            }
        }
    }
}
